

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="沧海横流，尽显英雄本色；激浊扬清，正是猛士当时">
  <meta name="author" content="closer">
  <meta name="keywords" content="">
  
  <title>使用Python控制鼠标、键盘和自动化操作 - closer的自留地</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.6.0/styles/androidstudio.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"blog.zsaa.top","root":"/","version":"1.8.10","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"always","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"baidu":"608f2baddd361128381ad2bf9377bf89","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"YzLqNtMw1YEwwACli1FUsIUM-gzGzoHsz","app_key":"HLUt5izfTvTcbEbOrA59W92a","server_url":"https://yzlqntmw.lc-cn-n1-shared.com"}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Hello</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="使用Python控制鼠标、键盘和自动化操作">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-11-23 21:05" pubdate>
        2021年11月23日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.6k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      38
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">使用Python控制鼠标、键盘和自动化操作</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2021年11月23日 晚上
                
              </p>
            
            <div class="markdown-body">
              <p>使用pyautogui这个库，来达到和按键精灵差不多的效果。可以运行在多平台。</p>
<p>PyAutoGUI的五个特点:</p>
<ul>
<li>移动鼠标点击或输入其他应用程序的窗口。</li>
<li>按键发送给应用程序(例如,填写表格)。</li>
<li>截图,给定一个图像(例如,一个按钮或复选框),在屏幕上找到它。</li>
<li>定位应用程序的窗口,和移动,调整,最大化、最小化、关闭(仅适用于windows,目前)</li>
<li>显示消息框进行用户交互,而您的GUI自动化脚本运行。</li>
</ul>
<h2 id="一：-安装pyautogui库"><a href="#一：-安装pyautogui库" class="headerlink" title="一： 安装pyautogui库"></a>一： 安装pyautogui库</h2><p><code>pip3 install pyautogui</code></p>
<p><strong>ubuntu系统，可能需要<code>sudo apt-get install python3-tk python3-dev</code>，如果安装不上，请参考<a target="_blank" rel="noopener" href="https://pyautogui.readthedocs.io/en/latest/install.html">官方文档如何安装</a></strong></p>
<h2 id="二：-使用方法"><a href="#二：-使用方法" class="headerlink" title="二： 使用方法"></a>二： 使用方法</h2><p><a target="_blank" rel="noopener" href="https://pyautogui.readthedocs.io/en/latest/">官方文档</a></p>
<h3 id="2-1-常规功能"><a href="#2-1-常规功能" class="headerlink" title="2.1 常规功能"></a>2.1 常规功能</h3><figure class="highlight py"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs py"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> pyautogui<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.position()  <span class="hljs-comment"># 当前鼠标 x， y坐标</span><br>(<span class="hljs-number">968</span>, <span class="hljs-number">56</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.size()  <span class="hljs-comment"># 当前屏幕分辨率宽和高</span><br>(<span class="hljs-number">1920</span>, <span class="hljs-number">1080</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.onScreen(x, y)  <span class="hljs-comment"># 鼠标坐标x且y，在屏幕内</span><br><span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.PAUSE = <span class="hljs-number">2.5</span> <span class="hljs-comment"># 在每次PyAutoGUI调用之后设置2.5秒的暂停：</span><br></code></pre></div></td></tr></table></figure>

<h3 id="2-2-鼠标功能"><a href="#2-2-鼠标功能" class="headerlink" title="2.2 鼠标功能"></a>2.2 鼠标功能</h3><figure class="highlight py"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs py"><span class="hljs-comment"># XY坐标在屏幕的左上角具有0、0的原点。X向右增加，Y向向下增加。</span><br><br><span class="hljs-comment"># 移动鼠标</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.moveTo(x, y, duration=num_seconds)  <span class="hljs-comment"># num秒内移动鼠标到XY坐标</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.move(xOffset, yOffset, duration=num_seconds)  <span class="hljs-comment"># 相对于当前位置，鼠标移动XY</span><br><span class="hljs-comment"># 如果duration为0或未指定，则立即移动，duration以秒为单位。</span><br><span class="hljs-comment"># 例： pyautogui.move(300, 200, 2)</span><br><br><span class="hljs-comment"># 拖拽鼠标</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.dragTo(x, y, duration=num_seconds)  <span class="hljs-comment"># 拖拽鼠标到XY</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.drag(xOffset, yOffset, duration=num_seconds)  <span class="hljs-comment"># 相对于当前位置，鼠标拖拽XY</span><br><span class="hljs-comment"># 例： pyautogui.drag(300, 200, 2)</span><br><br><span class="hljs-comment"># 点击鼠标</span><br><span class="hljs-comment"># 调用click()即在鼠标的当前位置左键单击一次，但是关键字参数可以改变它：</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button=<span class="hljs-string">&#x27;left&#x27;</span>)<br><span class="hljs-comment"># clicks关键字控制点击次数;interval关键字控制点击时间间隔。</span><br><span class="hljs-comment"># 该button关键字参数可以是&#x27;left&#x27;，&#x27;middle&#x27;或&#x27;right&#x27;。即左击，中击，右击。</span><br><span class="hljs-comment"># 例：pyautogui.click(300, 200,clicks=2,interval=3,button=&#x27;right&#x27;)</span><br><br><span class="hljs-comment"># 右击，中击及双、三击</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.rightClick(x=moveToX, y=moveToY)<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.middleClick(x=moveToX, y=moveToY)<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.doubleClick(x=moveToX, y=moveToY) <span class="hljs-comment"># 双击</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.tripleClick(x=moveToX, y=moveToY) <span class="hljs-comment"># 三击</span><br><br><span class="hljs-comment"># 鼠标中键滚动</span><br><span class="hljs-comment"># 正向滚动将向上滚动，负向滚动将向下滚动：</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.scroll(amount_to_scroll, x=moveToX, y=moveToY)<br><span class="hljs-comment"># 例： pyautogui.scroll(-10) </span><br><br><span class="hljs-comment"># 鼠标按下或者弹起：</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.mouseDown(x=moveToX, y=moveToY, button=<span class="hljs-string">&#x27;left&#x27;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.mouseUp(x=moveToX, y=moveToY, button=<span class="hljs-string">&#x27;left&#x27;</span>)<br></code></pre></div></td></tr></table></figure>

<h3 id="2-3-键盘功能"><a href="#2-3-键盘功能" class="headerlink" title="2.3 键盘功能"></a>2.3 键盘功能</h3><figure class="highlight py"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs py"><span class="hljs-comment"># 在键盘光标所在的位置按键。</span><br><br><span class="hljs-comment"># 输入字符串</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.write(<span class="hljs-string">&#x27;Hello world!\n&#x27;</span>, interval=secs_between_keys)  <span class="hljs-comment"># 用于输入文本，回车换行</span><br><span class="hljs-comment"># interval是字母输入间隔时间</span><br><br><span class="hljs-comment"># 依次按下键盘</span><br><span class="hljs-comment"># 也可以传递键名列表：</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.write([<span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;b&#x27;</span>, <span class="hljs-string">&#x27;c&#x27;</span>, <span class="hljs-string">&#x27;left&#x27;</span>, <span class="hljs-string">&#x27;backspace&#x27;</span>, <span class="hljs-string">&#x27;enter&#x27;</span>, <span class="hljs-string">&#x27;f1&#x27;</span>], interval=secs_between_keys)<br><span class="hljs-comment"># 键名的完整列表在pyautogui.KEYBOARD_KEYS中。</span><br><span class="hljs-comment"># 例： pyautogwrite([&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], interval=0.25)</span><br><br><span class="hljs-comment"># 多次按键</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.press([<span class="hljs-string">&#x27;left&#x27;</span>, <span class="hljs-string">&#x27;left&#x27;</span>, <span class="hljs-string">&#x27;left&#x27;</span>])<br><span class="hljs-comment"># 或者你可以设置按多少次左键 :</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.press(<span class="hljs-string">&#x27;1&#x27;</span>, presses=<span class="hljs-number">3</span>)<br><br><span class="hljs-comment"># 键盘快捷键（如Ctrl-S或Ctrl-Shift-1）可以通过将键名列表传递给来完成hotkey()：</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.hotkey(<span class="hljs-string">&#x27;ctrl&#x27;</span>, <span class="hljs-string">&#x27;c&#x27;</span>)  <span class="hljs-comment"># ctrl-c to copy</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.hotkey(<span class="hljs-string">&#x27;ctrl&#x27;</span>, <span class="hljs-string">&#x27;v&#x27;</span>)  <span class="hljs-comment"># ctrl-v to paste</span><br><br><span class="hljs-comment"># 键盘按下和弹起可以分别调用：</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.keyDown(key_name)<br><span class="hljs-meta">&gt;&gt;&gt; </span>pyautogui.keyUp(key_name)<br></code></pre></div></td></tr></table></figure>

<h3 id="2-4-消息框的功能"><a href="#2-4-消息框的功能" class="headerlink" title="2.4 消息框的功能"></a>2.4 消息框的功能</h3><figure class="highlight py"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs py"><span class="hljs-comment"># PyAutoGUI利用PyMsgBox消息框功能提供跨平台的,纯Python方法显示javascript式消息框。 有四个消息框功能。</span><br><br><span class="hljs-comment"># alert显示()函数 </span><br><span class="hljs-meta">&gt;&gt;&gt; </span>alert(text=<span class="hljs-string">&#x27;&#x27;</span>, title=<span class="hljs-string">&#x27;&#x27;</span>, button=<span class="hljs-string">&#x27;OK&#x27;</span>)<br><span class="hljs-comment"># 显示一个简单的消息框,文本和一个OK按钮。 返回文本的按钮点击。</span><br><span class="hljs-comment"># 例：pyautogui.alert(&#x27;这将显示一些文本和OK按钮.&#x27;)</span><br><br><span class="hljs-comment"># confirm确认()函数 </span><br><span class="hljs-meta">&gt;&gt;&gt; </span>confirm(text=<span class="hljs-string">&#x27;&#x27;</span>, title=<span class="hljs-string">&#x27;&#x27;</span>, buttons=[<span class="hljs-string">&#x27;OK&#x27;</span>, <span class="hljs-string">&#x27;Cancel&#x27;</span>])<br><span class="hljs-comment"># 显示一个消息框好和取消按钮。 数量和文本的按钮可以定制。 返回文本的按钮点击。</span><br><span class="hljs-comment"># 例： pyautogui.confirm(&#x27;显示文本和OK及取消键。&#x27;)</span><br><br><span class="hljs-comment"># prompt提示()函数 </span><br><span class="hljs-meta">&gt;&gt;&gt; </span>prompt(text=<span class="hljs-string">&#x27;&#x27;</span>, title=<span class="hljs-string">&#x27;&#x27;</span> , default=<span class="hljs-string">&#x27;&#x27;</span>)<br><span class="hljs-comment"># 显示一个消息框,文本输入,OK和Cancel按钮。 返回输入的文本,或没有,如果点击取消。</span><br><span class="hljs-comment"># 例： pyautogui.prompt(text=&#x27;显示文本输入框，OK及取消键&#x27;, title=&#x27;标题&#x27; , default=&#x27;&#x27;)</span><br><br><span class="hljs-comment"># password密码()函数 </span><br><span class="hljs-meta">&gt;&gt;&gt; </span>password(text=<span class="hljs-string">&#x27;&#x27;</span>, title=<span class="hljs-string">&#x27;&#x27;</span>, default=<span class="hljs-string">&#x27;&#x27;</span>, mask=<span class="hljs-string">&#x27;*&#x27;</span>)<br><span class="hljs-comment"># 显示一个消息框,文本输入,OK和Cancel按钮。 输入字符显示为 * 。 返回输入的文本,或没有,如果点击取消。</span><br><span class="hljs-comment"># 例： pyautogui.password(text=&#x27;显示文本输入框，OK及取消键&#x27;, title=&#x27;标题&#x27; , default=&#x27;默认密码&#x27; , mask=&#x27;*&#x27;)</span><br></code></pre></div></td></tr></table></figure>

<h3 id="2-5-屏幕截图功能"><a href="#2-5-屏幕截图功能" class="headerlink" title="2.5 屏幕截图功能"></a>2.5 屏幕截图功能</h3><p>在ubuntu系统中支持的不是很好，比较复杂。<br><a target="_blank" rel="noopener" href="https://pyautogui.readthedocs.io/en/latest/screenshot.html">具体参照官方文档</a></p>
<p>还可以根据色块或图片返回对应的坐标。这个就比较强大啦。比如可以制作自动游戏脚本等等。</p>
<h2 id="三：-实战演练"><a href="#三：-实战演练" class="headerlink" title="三： 实战演练"></a>三： 实战演练</h2><h3 id="3-1-实时显示鼠标位置"><a href="#3-1-实时显示鼠标位置" class="headerlink" title="3.1 实时显示鼠标位置"></a>3.1 实时显示鼠标位置</h3><figure class="highlight py"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs py"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><br><span class="hljs-keyword">import</span> pyautogui, sys<br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;Press Ctrl-C to quit.&#x27;</span>)<br><span class="hljs-keyword">try</span>:<br>    <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>        x, y = pyautogui.position()<br>        positionStr = <span class="hljs-string">&#x27;X: &#x27;</span> + <span class="hljs-built_in">str</span>(x).rjust(<span class="hljs-number">4</span>) + <span class="hljs-string">&#x27; Y: &#x27;</span> + <span class="hljs-built_in">str</span>(y).rjust(<span class="hljs-number">4</span>)<br>        <span class="hljs-built_in">print</span>(positionStr, end=<span class="hljs-string">&#x27;&#x27;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;\b&#x27;</span> * <span class="hljs-built_in">len</span>(positionStr), end=<span class="hljs-string">&#x27;&#x27;</span>, flush=<span class="hljs-literal">True</span>)<br><span class="hljs-keyword">except</span> KeyboardInterrupt:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;\n&#x27;</span>)<br></code></pre></div></td></tr></table></figure>

<h3 id="3-2-画图软件中画出方形螺旋图"><a href="#3-2-画图软件中画出方形螺旋图" class="headerlink" title="3.2 画图软件中画出方形螺旋图"></a>3.2 画图软件中画出方形螺旋图</h3><figure class="highlight py"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs py"><span class="hljs-comment">#!/usr/bin/python3</span><br><span class="hljs-comment"># -*- coding: utf-8 -*-</span><br><br><span class="hljs-keyword">import</span> pyautogui<br>distance = <span class="hljs-number">200</span><br><span class="hljs-keyword">while</span> distance &gt; <span class="hljs-number">0</span>:<br>        pyautogui.drag(distance, <span class="hljs-number">0</span>, duration=<span class="hljs-number">0.5</span>)   <span class="hljs-comment"># move right</span><br>        distance -= <span class="hljs-number">5</span><br>        pyautogui.drag(<span class="hljs-number">0</span>, distance, duration=<span class="hljs-number">0.5</span>)   <span class="hljs-comment"># move down</span><br>        pyautogui.drag(-distance, <span class="hljs-number">0</span>, duration=<span class="hljs-number">0.5</span>)  <span class="hljs-comment"># move left</span><br>        distance -= <span class="hljs-number">5</span><br>        pyautogui.drag(<span class="hljs-number">0</span>, -distance, duration=<span class="hljs-number">0.5</span>)  <span class="hljs-comment"># move up</span><br></code></pre></div></td></tr></table></figure>

<h3 id="3-3-自动化软件-BY-不高兴就喝水"><a href="#3-3-自动化软件-BY-不高兴就喝水" class="headerlink" title="3.3 自动化软件 BY 不高兴就喝水"></a>3.3 自动化软件 BY <a target="_blank" rel="noopener" href="https://space.bilibili.com/412704776">不高兴就喝水</a></h3><p>1、首先新建个名称为<code>cmd.xls</code>的excel文件，按如下格式输入操作内容；随后将点击步骤的图片保存为<code>.png</code>格式并保存在同一文件夹。</p>
<table>
<thead>
<tr>
<th>指令类型（1 单击 2 双击   3 右键 4 输入 5 等待   6滚轮（一般取-30））</th>
<th>内容（图片名称.png、输入内容、等待时长/秒）</th>
<th>重复次数(-1代表一直重复)</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>WeiJian.png</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>0.1</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td>ZhengChang.png</td>
<td></td>
</tr>
</tbody></table>
<p>2、在CMD中安装<code>Pip install xlrd pyautogui==0.9.50 opencv-python pillow</code></p>
<p>3、复制如下代码，黏贴为<code>.py</code>后缀的文件并执行。</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pyautogui<br><span class="hljs-keyword">import</span> time<br><span class="hljs-keyword">import</span> xlrd<br><span class="hljs-keyword">import</span> pyperclip<br><span class="hljs-keyword">import</span> sys<br><br><span class="hljs-comment">#定义鼠标事件</span><br><br><span class="hljs-comment">#pyautogui库其他用法 https://blog.csdn.net/qingfengxd1/article/details/108270159</span><br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">mouseClick</span>(<span class="hljs-params">clickTimes,lOrR,img,reTry</span>):</span><br>    <span class="hljs-keyword">if</span> reTry == <span class="hljs-number">1</span>:<br>        <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>            location=pyautogui.locateCenterOnScreen(img,confidence=<span class="hljs-number">0.9</span>)<br>            <span class="hljs-keyword">if</span> location <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:<br>                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=<span class="hljs-number">0.1</span>,duration=<span class="hljs-number">0.1</span>,button=lOrR)<br>                <span class="hljs-keyword">break</span><br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;未找到匹配图片,0.1秒后重试&quot;</span>)<br>            time.sleep(<span class="hljs-number">0.1</span>)<br>    <span class="hljs-keyword">elif</span> reTry == -<span class="hljs-number">1</span>:<br>        <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>            location=pyautogui.locateCenterOnScreen(img,confidence=<span class="hljs-number">0.9</span>)<br>            <span class="hljs-keyword">if</span> location <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:<br>                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=<span class="hljs-number">0.1</span>,duration=<span class="hljs-number">0.1</span>,button=lOrR)<br>            time.sleep(<span class="hljs-number">0.1</span>)<br>    <span class="hljs-keyword">elif</span> reTry &gt; <span class="hljs-number">1</span>:<br>        i = <span class="hljs-number">1</span><br>        <span class="hljs-keyword">while</span> i &lt; reTry + <span class="hljs-number">1</span>:<br>            location=pyautogui.locateCenterOnScreen(img,confidence=<span class="hljs-number">0.9</span>)<br>            <span class="hljs-keyword">if</span> location <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:<br>                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=<span class="hljs-number">0.1</span>,duration=<span class="hljs-number">0.1</span>,button=lOrR)<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;重复&quot;</span>)<br>                i += <span class="hljs-number">1</span><br>            time.sleep(<span class="hljs-number">0.1</span>)<br><br><br><span class="hljs-comment"># 数据检查</span><br><span class="hljs-comment"># cmdType.value  1.0 左键单击    2.0 左键双击  3.0 右键单击  4.0 输入  5.0 等待  6.0 滚轮</span><br><span class="hljs-comment"># ctype     空：0</span><br><span class="hljs-comment">#           字符串：1</span><br><span class="hljs-comment">#           数字：2</span><br><span class="hljs-comment">#           日期：3</span><br><span class="hljs-comment">#           布尔：4</span><br><span class="hljs-comment">#           error：5</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dataCheck</span>(<span class="hljs-params">sheet1</span>):</span><br>    checkCmd = <span class="hljs-literal">True</span><br>    <span class="hljs-comment">#行数检查</span><br>    <span class="hljs-keyword">if</span> sheet1.nrows&lt;<span class="hljs-number">2</span>:<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;没数据啊哥&quot;</span>)<br>        checkCmd = <span class="hljs-literal">False</span><br>    <span class="hljs-comment">#每行数据检查</span><br>    i = <span class="hljs-number">1</span><br>    <span class="hljs-keyword">while</span> i &lt; sheet1.nrows:<br>        <span class="hljs-comment"># 第1列 操作类型检查</span><br>        cmdType = sheet1.row(i)[<span class="hljs-number">0</span>]<br>        <span class="hljs-keyword">if</span> cmdType.ctype != <span class="hljs-number">2</span> <span class="hljs-keyword">or</span> (cmdType.value != <span class="hljs-number">1.0</span> <span class="hljs-keyword">and</span> cmdType.value != <span class="hljs-number">2.0</span> <span class="hljs-keyword">and</span> cmdType.value != <span class="hljs-number">3.0</span> <br>        <span class="hljs-keyword">and</span> cmdType.value != <span class="hljs-number">4.0</span> <span class="hljs-keyword">and</span> cmdType.value != <span class="hljs-number">5.0</span> <span class="hljs-keyword">and</span> cmdType.value != <span class="hljs-number">6.0</span>):<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;第&#x27;</span>,i+<span class="hljs-number">1</span>,<span class="hljs-string">&quot;行,第1列数据有毛病&quot;</span>)<br>            checkCmd = <span class="hljs-literal">False</span><br>        <span class="hljs-comment"># 第2列 内容检查</span><br>        cmdValue = sheet1.row(i)[<span class="hljs-number">1</span>]<br>        <span class="hljs-comment"># 读图点击类型指令，内容必须为字符串类型</span><br>        <span class="hljs-keyword">if</span> cmdType.value ==<span class="hljs-number">1.0</span> <span class="hljs-keyword">or</span> cmdType.value == <span class="hljs-number">2.0</span> <span class="hljs-keyword">or</span> cmdType.value == <span class="hljs-number">3.0</span>:<br>            <span class="hljs-keyword">if</span> cmdValue.ctype != <span class="hljs-number">1</span>:<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;第&#x27;</span>,i+<span class="hljs-number">1</span>,<span class="hljs-string">&quot;行,第2列数据有毛病&quot;</span>)<br>                checkCmd = <span class="hljs-literal">False</span><br>        <span class="hljs-comment"># 输入类型，内容不能为空</span><br>        <span class="hljs-keyword">if</span> cmdType.value == <span class="hljs-number">4.0</span>:<br>            <span class="hljs-keyword">if</span> cmdValue.ctype == <span class="hljs-number">0</span>:<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;第&#x27;</span>,i+<span class="hljs-number">1</span>,<span class="hljs-string">&quot;行,第2列数据有毛病&quot;</span>)<br>                checkCmd = <span class="hljs-literal">False</span><br>        <span class="hljs-comment"># 等待类型，内容必须为数字</span><br>        <span class="hljs-keyword">if</span> cmdType.value == <span class="hljs-number">5.0</span>:<br>            <span class="hljs-keyword">if</span> cmdValue.ctype != <span class="hljs-number">2</span>:<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;第&#x27;</span>,i+<span class="hljs-number">1</span>,<span class="hljs-string">&quot;行,第2列数据有毛病&quot;</span>)<br>                checkCmd = <span class="hljs-literal">False</span><br>        <span class="hljs-comment"># 滚轮事件，内容必须为数字</span><br>        <span class="hljs-keyword">if</span> cmdType.value == <span class="hljs-number">6.0</span>:<br>            <span class="hljs-keyword">if</span> cmdValue.ctype != <span class="hljs-number">2</span>:<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;第&#x27;</span>,i+<span class="hljs-number">1</span>,<span class="hljs-string">&quot;行,第2列数据有毛病&quot;</span>)<br>                checkCmd = <span class="hljs-literal">False</span><br>        i += <span class="hljs-number">1</span><br>    <span class="hljs-keyword">return</span> checkCmd<br><br><span class="hljs-comment">#任务</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">mainWork</span>(<span class="hljs-params">img</span>):</span><br>    i = <span class="hljs-number">1</span><br>    <span class="hljs-keyword">while</span> i &lt; sheet1.nrows:<br>        <span class="hljs-comment">#取本行指令的操作类型</span><br>        cmdType = sheet1.row(i)[<span class="hljs-number">0</span>]<br>        <span class="hljs-keyword">if</span> cmdType.value == <span class="hljs-number">1.0</span>:<br>            <span class="hljs-comment">#取图片名称</span><br>            img = sheet1.row(i)[<span class="hljs-number">1</span>].value<br>            reTry = <span class="hljs-number">1</span><br>            <span class="hljs-keyword">if</span> sheet1.row(i)[<span class="hljs-number">2</span>].ctype == <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> sheet1.row(i)[<span class="hljs-number">2</span>].value != <span class="hljs-number">0</span>:<br>                reTry = sheet1.row(i)[<span class="hljs-number">2</span>].value<br>            mouseClick(<span class="hljs-number">1</span>,<span class="hljs-string">&quot;left&quot;</span>,img,reTry)<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;单击左键&quot;</span>,img)<br>        <span class="hljs-comment">#2代表双击左键</span><br>        <span class="hljs-keyword">elif</span> cmdType.value == <span class="hljs-number">2.0</span>:<br>            <span class="hljs-comment">#取图片名称</span><br>            img = sheet1.row(i)[<span class="hljs-number">1</span>].value<br>            <span class="hljs-comment">#取重试次数</span><br>            reTry = <span class="hljs-number">1</span><br>            <span class="hljs-keyword">if</span> sheet1.row(i)[<span class="hljs-number">2</span>].ctype == <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> sheet1.row(i)[<span class="hljs-number">2</span>].value != <span class="hljs-number">0</span>:<br>                reTry = sheet1.row(i)[<span class="hljs-number">2</span>].value<br>            mouseClick(<span class="hljs-number">2</span>,<span class="hljs-string">&quot;left&quot;</span>,img,reTry)<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;双击左键&quot;</span>,img)<br>        <span class="hljs-comment">#3代表右键</span><br>        <span class="hljs-keyword">elif</span> cmdType.value == <span class="hljs-number">3.0</span>:<br>            <span class="hljs-comment">#取图片名称</span><br>            img = sheet1.row(i)[<span class="hljs-number">1</span>].value<br>            <span class="hljs-comment">#取重试次数</span><br>            reTry = <span class="hljs-number">1</span><br>            <span class="hljs-keyword">if</span> sheet1.row(i)[<span class="hljs-number">2</span>].ctype == <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> sheet1.row(i)[<span class="hljs-number">2</span>].value != <span class="hljs-number">0</span>:<br>                reTry = sheet1.row(i)[<span class="hljs-number">2</span>].value<br>            mouseClick(<span class="hljs-number">1</span>,<span class="hljs-string">&quot;right&quot;</span>,img,reTry)<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;右键&quot;</span>,img) <br>        <span class="hljs-comment">#4代表输入</span><br>        <span class="hljs-keyword">elif</span> cmdType.value == <span class="hljs-number">4.0</span>:<br>            inputValue = sheet1.row(i)[<span class="hljs-number">1</span>].value<br>            pyperclip.copy(inputValue)<br>            pyautogui.hotkey(<span class="hljs-string">&#x27;ctrl&#x27;</span>,<span class="hljs-string">&#x27;v&#x27;</span>)<br>            time.sleep(<span class="hljs-number">0.5</span>)<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;输入:&quot;</span>,inputValue)                                        <br>        <span class="hljs-comment">#5代表等待</span><br>        <span class="hljs-keyword">elif</span> cmdType.value == <span class="hljs-number">5.0</span>:<br>            <span class="hljs-comment">#取图片名称</span><br>            waitTime = sheet1.row(i)[<span class="hljs-number">1</span>].value<br>            time.sleep(waitTime)<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;等待&quot;</span>,waitTime,<span class="hljs-string">&quot;秒&quot;</span>)<br>        <span class="hljs-comment">#6代表滚轮，一般取-30</span><br>        <span class="hljs-keyword">elif</span> cmdType.value == <span class="hljs-number">6.0</span>:<br>            <span class="hljs-comment">#取图片名称</span><br>            scroll = sheet1.row(i)[<span class="hljs-number">1</span>].value<br>            pyautogui.scroll(<span class="hljs-built_in">int</span>(scroll))<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;滚轮滑动&quot;</span>,<span class="hljs-built_in">int</span>(scroll),<span class="hljs-string">&quot;距离&quot;</span>)                      <br>        i += <span class="hljs-number">1</span><br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    file = <span class="hljs-string">&#x27;cmd.xls&#x27;</span><br>    <span class="hljs-comment">#打开文件</span><br>    wb = xlrd.open_workbook(filename=file)<br>    <span class="hljs-comment">#通过索引获取表格sheet页</span><br>    sheet1 = wb.sheet_by_index(<span class="hljs-number">0</span>)<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;欢迎使用自动化操作脚本&#x27;</span>)<br>    <span class="hljs-comment">#数据检查</span><br>    checkCmd = dataCheck(sheet1)<br>    <span class="hljs-keyword">if</span> checkCmd:<br>        key=<span class="hljs-built_in">int</span>(<span class="hljs-built_in">input</span>(<span class="hljs-string">&#x27;输入数字选择循环几次，0则循环到死，负数则退出 \n&#x27;</span>))<br>        <span class="hljs-comment">#循环拿出每一行指令</span><br>        <span class="hljs-keyword">if</span>  key&gt;<span class="hljs-number">0</span>:<br>            <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>,key):<br>                mainWork(sheet1)<br>        <span class="hljs-keyword">elif</span> key==<span class="hljs-number">0</span>:<br>            <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>                mainWork(sheet1)<br>                time.sleep(<span class="hljs-number">0.2</span>)<br>                <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;等待0.2秒&quot;</span>)<br>        <span class="hljs-keyword">elif</span> key&lt;<span class="hljs-number">0</span>:<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;即将退出...&quot;</span>)<br>            sys.exit(<span class="hljs-number">0</span>)<br>    <span class="hljs-keyword">else</span>:<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;输入有误或者已经退出!&#x27;</span>)<br></code></pre></div></td></tr></table></figure>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/python/">python</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/python/">python</a>
                    
                      <a class="hover-with-bg" href="/tags/%E6%8C%89%E9%94%AE%E7%B2%BE%E7%81%B5/">按键精灵</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/22/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Python/%E9%87%8F%E5%8C%96%E4%B8%ADPandas%E5%BA%93%E7%9A%84%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E6%8A%80%E5%B7%A7/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">量化中Pandas库的常用函数技巧</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/14/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">
                        <span class="hidden-mobile">操作系统</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#valine",
          app_id: "YzLqNtMw1YEwwACli1FUsIUM-gzGzoHsz",
          app_key: "HLUt5izfTvTcbEbOrA59W92a",
          placeholder: "畅所欲言...",
          path: window.location.pathname,
          avatar: "robohash",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->

  <div class="col-lg-7 mx-auto nopadding-x-md">
    <div class="container custom post-custom mx-auto">
      <img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/pay/pay.png" srcset="/img/loading.gif" lazyload class="rounded mx-auto d-block mt-3" style="width:355.4px; height:200px;">
    </div>
  </div>


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- LeanCloud 统计PV -->
        <span id="leancloud-site-pv-container" style="display: none">
            总访问量 
            <span id="leancloud-site-pv"></span>
             次
          </span>
      
      
        <!-- LeanCloud 统计UV -->
        <span id="leancloud-site-uv-container" style="display: none">
            总访客数 
            <span id="leancloud-site-uv"></span>
             人
          </span>
      

    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        苏ICP备20032307号
      </a>
    </span>
    
      
        <span>
          <a
            href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=32020602001023"
            rel="nofollow noopener"
            class="beian-police"
            target="_blank"
          >
            
              <span style="visibility: hidden; width: 0">|</span>
              <img src="/img/police_beian.png" srcset="/img/loading.gif" lazyload alt="police-icon"/>
            
            <span>苏公网安备 32020602001023号</span>
          </a>
        </span>
      
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.2/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>




  <script defer src="/js/leancloud.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      $('#local-search-input').on('click', function() {
        searchFunc(path, 'local-search-input', 'local-search-result');
      });
      $('#modalSearch').on('shown.bs.modal', function() {
        $('#local-search-input').focus();
      });
    })()
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js" ></script>

  








  
    <!-- Baidu Analytics -->
    <script defer>
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?608f2baddd361128381ad2bf9377bf89";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
